import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/components/layout/index.vue'

import workbench from './modules/workbench'
import author from './modules/author'

Vue.use(VueRouter)

const baseRoutes = [
  {
    path: '/redirect/:path*',
    name: 'redirect',
    component: () => import(/* webpackChunkName: 'base' */ '@/views/redirect'),
    hidden: true
  },
  {
    path: '/',
    name: '_home',
    redirect: '/redirect',
    component: Layout,
    meta: {
      title: '首页',
      icon: ''
    }
  },
  {
    path: '/home',
    name: 'home',
    component: Layout,
    meta: {
      title: '首页',
      icon: ''
    }
  },
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: 'base' */ '@/views/login/index.vue')
  },
  {
    path: '/register',
    name: 'register',
    component: () => import(/* webpackChunkName: 'base' */ '@/views/register/index.vue')
  }
]

export const routes = [...workbench, ...author]

const createRouter = () =>
  new VueRouter({
    mode: 'hash',
    base: process.env.BASE_URL,
    scrollBehavior: () => ({ y: 0 }),
    routes: [
      ...baseRoutes,
      ...routes,
      {
        path: '*',
        redirect: '/redirect'
      }
    ]
  })

/**
 * 重置路由
 * Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
 */
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

const router = createRouter()

export default router
